package com.itboye.lottery.fragment.user;


import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.handmark.pulltorefresh.library.ObservableScrollView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.itboye.lottery.base.BaseFragment;
import com.itboye.lottery.R;
import com.itboye.lottery.adapter.MessageListAdapter;
import com.itboye.lottery.alert.ByAlert;
import com.itboye.lottery.bean.MessageBean;
import com.itboye.lottery.presenter.UserPresenter;
import com.itboye.lottery.retrofit.ResponseCallback;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MessageFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MessageFragment extends BaseFragment implements ResponseCallback {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private int mParam1;
    private String mParam2;


    UserPresenter userPresenter;
    RecyclerView rv_message;
    private MessageListAdapter adapter;
    ImageView iv_messgage_empty;
    PullToRefreshScrollView pull_to_refresh_scrollView;
    private int page = 1;
    private List<MessageBean> messageBeanList = new ArrayList<>();

    public MessageFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MessageFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MessageFragment newInstance(int param1, String param2) {
        MessageFragment fragment = new MessageFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_message;
    }

    @Override
    protected void initData() {
        userPresenter = new UserPresenter(this);
        if (getArguments() != null) {
            mParam1 = getArguments().getInt(ARG_PARAM1, 0);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        adapter = new MessageListAdapter(getActivity(), messageBeanList);
        rv_message.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv_message.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        rv_message.setAdapter(adapter);
        pull_to_refresh_scrollView.setMode(PullToRefreshBase.Mode.BOTH);
        pull_to_refresh_scrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ObservableScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ObservableScrollView> refreshView) {
                page = 1;
                getData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ObservableScrollView> refreshView) {
                page++;
                getData();
            }
        });
        getData();
    }

    private void getData() {
        userPresenter.messageQuery(mParam1, page);
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void success(@NotNull Object o, @NotNull String requestSuccessType) {
        pull_to_refresh_scrollView.onRefreshComplete();
        if (requestSuccessType == userPresenter.messageQuery_SUCCESS) {
//            List<MessageBean> messageBeanList = ;
//            if (messageBeanList == null || messageBeanList.isEmpty()) {
//                setMessageEmpty(true);
//            } else {
            setMessageEmpty(false);
            if (page == 1) {
                this.messageBeanList.clear();
            }
            this.messageBeanList.addAll((List<MessageBean>) o);
            adapter.notifyDataSetChanged();
        }
//        }
    }

    private void setMessageEmpty(boolean b) {
        if (b) {
            rv_message.setVisibility(View.GONE);
            iv_messgage_empty.setVisibility(View.VISIBLE);
        } else {
            rv_message.setVisibility(View.VISIBLE);
            iv_messgage_empty.setVisibility(View.GONE);
        }
    }

    @Override
    public void fail(int code, @NotNull Object msg, @NotNull String requestErrorType) {
        pull_to_refresh_scrollView.onRefreshComplete();
        ByAlert.alert(msg);
    }
}
